package com.googlecode.mailnet.emf.netmodel.visualization.highrisk;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Shape;
import org.eclipse.swt.graphics.Color;

import com.googlecode.mailnet.emf.netmodel.simulation.LinkStatistics;
import com.googlecode.mailnet.emf.netmodel.simulation.SimulationContext;
import com.googlecode.mailnet.emf.netmodel.visualization.ShapeDecorator;
import com.googlecode.mailnet.info.ILinkInfo;

public class HighRiskLinkDecorator extends ShapeDecorator {
	ILinkInfo linkInfo;
	SimulationContext context;
	
	public HighRiskLinkDecorator(ILinkInfo linkInfo, SimulationContext context) {
		this.linkInfo = linkInfo;
		this.context = context;
	}
	
	public boolean outlineShape(Shape shape, Graphics g) {
		LinkStatistics linkStat = context.getStatistics().getLinkStats(linkInfo.getLink());
		double dataTotal = linkStat.dataFailed + linkStat.dataTransmitted;
		double risk = ((double)linkStat.dataFailed) / dataTotal;
		if (risk > 0.05) risk = 0.05;
		risk *= 20.0;
		double importance = linkStat.importance;
		//double timeNormalizer = linkStat.completeTimeOnFull > 0 ? (double)linkStat.timeLinkIsFull / (double)linkStat.completeTimeOnFull : 1.0;
		
		long colorBrightness = Math.round(importance * risk * 85.0);
		int width = (int)Math.round(importance * risk * 5.0);
		Color color = new Color(null, 170 + (int)colorBrightness, 170 - (int)colorBrightness * 2, 170 - (int)colorBrightness * 2);
		shape.setForegroundColor(color);
		shape.setLineStyle(linkInfo.isBroken() ? Graphics.LINE_DASH : Graphics.LINE_SOLID);
		shape.setLineWidth(width);
		return false;
	}
}
